package com.ctsi.ftp.thread;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

import com.ctsi.ftp.util.FTPUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.Path;

import com.ctsi.ftp.util.ConfigProperties;


public class AccountAlarmTask implements Runnable,Serializable{
	private static Log log = LogFactory.getLog(AccountAlarmTask.class);
	
	private static final long serialVersionUID = 0;
	
	private String taskId;
	private Path[] files;
	private String ftpUrl;
	private int ftpPort;
	private String ftpUserName;
	private String ftpPassword;
	private String remotePath;
	private String successHdfsPath;
	private String fielPostfix;
	private CountDownLatch countDownLatch;
	private boolean isGZip;

	public AccountAlarmTask(String taskId, Path[] files, String ftpUrl, int ftpPort, String ftpUserName, String ftpPassword
            , String remotePath, String successHdfsPath, String fielPostfix, boolean isGZip, CountDownLatch countDownLatch){
		this.taskId = taskId;
	//	this.files = files;
		this.files = files;
		this.ftpUrl = ftpUrl;
		this.ftpPort = ftpPort;
		this.ftpUserName = ftpUserName;
		this.ftpPassword = ftpPassword;
		this.remotePath = remotePath;
		this.successHdfsPath = successHdfsPath;
		this.fielPostfix = fielPostfix;
		this.isGZip = isGZip;
		this.countDownLatch = countDownLatch;
		log.debug((new Date(System.currentTimeMillis()).toLocaleString())+",create AccountAlarmTask ["+taskId+"]");
	}
	
	public String getTaskId(){
		return this.taskId;
	}
	@Override
	public void run() {
		try {
			log.debug((new Date(System.currentTimeMillis()).toLocaleString())+",begin AccountAlarmTask ["+taskId+"]");
			String isUseSunFtp = ConfigProperties.getProperty("isUseSunFtp");
			if(isUseSunFtp==null||!"true".equals(isUseSunFtp)){
					//SXS:for test******************************************************************************************************************
					//FTPUtil.uploadFileByDirectory(ftpUrl, ftpPort, ftpUserName, ftpPassword, remotePath, files, successHdfsPath, fielPostfix, isGZip);
				//SXS:debug****************
				//System.out.println(taskId);
				FTPUtil.uploadFiles(ftpUrl, ftpPort, ftpUserName, ftpPassword, remotePath, files, successHdfsPath, fielPostfix, isGZip);
			}else{
				//FtpUtilNew.uploadFileByDirectory(ftpUrl, ftpPort, ftpUserName, ftpPassword, remotePath, files, successLocalPath, fielPostfix, isGZip);
			}
				
			log.debug(taskId+" countDownLatch count :" +countDownLatch.getCount());
			log.debug((new Date(System.currentTimeMillis()).toLocaleString())+",end AccountAlarmTask ["+taskId+"]");
		} catch (Exception e) {
			e.printStackTrace();
			log.error((new Date(System.currentTimeMillis()).toLocaleString())+",error AccountAlarmTask ["+taskId+"]");
		} finally{
			countDownLatch.countDown();
			log.debug((new Date(System.currentTimeMillis()).toLocaleString())+",end AccountAlarmTask ["+taskId+"]");
		}
	}

	@Override
	public String toString() {
		
		return taskId;
	}
	

}
